<?php

/* * *
 * @Author:vampireC
 * @Copyright:http://www.congshan.net
 * $time:201402211301$
 * $md5:$
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * * */

function GET($n) {
    return isset($_GET[$n]) ? $_GET[$n] : NULL;
}

function _get($n) {
    return isset($_GET[$n]) ? $_GET[$n] : NULL;
}

$vcode = "";
$sz = GET('sz'); // 获得字体大小
if ($sz == 0) {
    $sz = 15; //默认字体大小15
} elseif ($sz > 50) {//防止恶意访问
    $sz = 15; //默认字体大小15
}
$w = $sz * 6;
$h = $sz * 2;
$im = 'img';
$$im = imagecreatetruecolor($w, $h); //创建一个真彩图
$string = "345679ABCDEFGHJKLMNPRSTUVWXY"; //字符串，去了掉容易混淆的字符
$white = imagecolorallocate($$im, 255, 255, 255); //分配白色
$black = imagecolorallocate($$im, 0, 0, 0); //分配黑色
imagefilledrectangle($$im, 0, 0, $w, $h, $white); //画布背景颜色
$ttf = 'vcode.ttf'; //设置字体
$length = mt_rand(3, 5);
for ($i = 0; $i < $length; $i++) {
    $code = mb_substr($string, mt_rand(0, 27), 1);
    $vcode.=$code;
}
//写背景干扰
//为便于识别当字体大小小于14时关闭干扰文字
if ($sz > 14) {
    for ($i = 0; $i < 5; $i++) {
        $nosiecolor = imagecolorallocate($$im, mt_rand(100, 225), mt_rand(100, 225), mt_rand(100, 225));
        imagestring($$im, 3, mt_rand(0, $w), mt_rand(0, $h), $vcode, $nosiecolor);
    }
}
for ($i = 0; $i < $sz * 2; $i++) {
    imagechar($$im, 2, mt_rand(0, $w), mt_rand(0, $h), ".", $black);
}
//写入验证码
$angle = mt_rand(-3, 3);
$x = $sz / 2;
$y = $h - 5;
imagettftext($$im, $sz, $angle, $x, $y, $black, $ttf, $vcode);
//写入一条干扰直线 如果要增加条数直接复制即可
imageline($$im, mt_rand(0, 10), mt_rand(0, $h), mt_rand(10, $w), mt_rand(10, $h), $black);
imageline($$im, mt_rand(0, 10), mt_rand(0, $w), mt_rand(10, $h), mt_rand(10, $h), $black);
imageline($$im, mt_rand(0, 10), mt_rand(0, $h), mt_rand(10, $w), mt_rand(10, $h), $black);
imageline($$im, mt_rand(0, 10), mt_rand(0, $h), mt_rand(10, $w), mt_rand(10, $h), $black);
//列如 去掉这行注释符 imageline($$im,mt_rand(0,10),mt_rand(0,$h),mt_rand(10,$w),mt_rand(10,$h),$black);
//写入会话
if (!isset($_SESSION))
    session_start();

switch (_get("discern")) {
    case "a":$discern = "vcode_a";
        break;
    case "b":$discern = "vcode_b";
        break;
    case "c":$discern = "vcode_c";
        break;
    default :$discern = "vcode";
}
$_SESSION[$discern] = ""; //初始化
$_SESSION[$discern] = strtolower($vcode);
//print_r($GLOBALS);
header("Content-type:image/jpeg");
imagejpeg($$im);
imagedestroy($$im);
?>